package com.cognizant.ams.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cognizant.ams.bean.Lastatsegment;
import com.cognizant.ams.bean.LastatsegmentExample;
import com.cognizant.ams.common.util.CognizantException;
import com.cognizant.ams.dao.LastatsegmentMapper;
import com.cognizant.ams.service.SettlementService;
import com.cognizant.ams.bean.vo.SettlementQueryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
//import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author suyon
 * @date 2019-11-11 14:03
 */
@Service
//@Slf4j
public class SettlementServiceImpl implements SettlementService {
    @Autowired
    private LastatsegmentMapper lastatsegmentMapper;

    @Override
    public List<Lastatsegment> getList(SettlementQueryVO queryVO) {
        LastatsegmentExample lastatsegmentExample = new LastatsegmentExample();
        LastatsegmentExample.Criteria param = lastatsegmentExample.createCriteria();

        if (StringUtil.isNotEmpty(queryVO.getStartTime())) {
            param.andYearmonthGreaterThanOrEqualTo(queryVO.getStartTime());
        }

        if (StringUtil.isNotEmpty(queryVO.getEndTime())) {
            param.andYearmonthLessThanOrEqualTo(queryVO.getEndTime());
        }

        lastatsegmentExample.setOrderByClause("yearMonth");

        return lastatsegmentMapper.selectByExample(lastatsegmentExample);
    }

    @Override
    public PageInfo pageQuery(SettlementQueryVO queryVO) {
//        //log.info("财务月结查询条件:{}", JSONObject.toJSON(queryVO));

        PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<Lastatsegment> list = getList(queryVO);
        PageInfo<Lastatsegment> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public void saveOrUpdateByExcelList(List<Lastatsegment> list) {

        if(CollectionUtils.isEmpty(list)){
            throw new CognizantException("没有任何编辑项目,请重新确认");
        }

        Set yartMonthSet = new HashSet();
        List paramYearMonth = new ArrayList();
        Set managecomSet = new HashSet();
        List paramManageCom = new ArrayList();
        Set statTypeSet = new HashSet();
        List paramstatType = new ArrayList();

        list.forEach(lastatsegment->{
            managecomSet.add(lastatsegment.getManagecom());
            yartMonthSet.add(lastatsegment.getYearmonth());
            statTypeSet.add(lastatsegment.getStattype());
        });

        paramYearMonth.addAll(yartMonthSet);
        paramstatType.addAll(statTypeSet);
        paramManageCom.addAll(managecomSet);


        LastatsegmentExample delExample = new LastatsegmentExample();
        LastatsegmentExample.Criteria delParam = delExample.createCriteria();
        delParam.andYearmonthIn(paramYearMonth);
        delParam.andManagecomIn(paramManageCom);
        delParam.andStattypeIn(paramstatType);

        try{
            lastatsegmentMapper.deleteByExample(delExample);

            list.forEach(insertNode->{
                lastatsegmentMapper.insert(insertNode);
            });
        }catch(Exception e){
            throw new CognizantException("编辑失败");
        }

    }
}
